package org.jeecg.modules.work.sweep.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.constant.BikeConstant;
import org.jeecg.modules.utils.ExcelUtils;
import org.jeecg.modules.work.file.entity.BikeWorkSubmitFile;
import org.jeecg.modules.work.file.mapper.BikeWorkSubmitFileMapper;
import org.jeecg.modules.work.repair.entity.BikeWorkRepair;
import org.jeecg.modules.work.sweep.entity.BikeWorkSweep;
import org.jeecg.modules.work.sweep.mapper.BikeWorkSweepMapper;
import org.jeecg.modules.work.sweep.service.IBikeWorkSweepService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

/**
 * @Description: 保洁工单
 * @Author: 月月鸟
 * @Date:   2021-05-31
 * @Version: V1.0
 */
@Service
public class BikeWorkSweepServiceImpl extends ServiceImpl<BikeWorkSweepMapper, BikeWorkSweep> implements IBikeWorkSweepService {
    @Resource
    private BikeWorkSweepMapper bikeWorkSweepMapper;
    @Resource
    private BikeWorkSubmitFileMapper bikeWorkSubmitFileMapper;
    @Value("${application.domain}")
    public String domain;
    @Override
    public Result<?> pageList(Integer pageNo, Integer pageSize, BikeWorkSweep bikeWorkSweep) {
        List<BikeWorkSweep> bikeWorkSweeps = bikeWorkSweepMapper.pageList(bikeWorkSweep);
        for (BikeWorkSweep sf : bikeWorkSweeps) {
            QueryWrapper<BikeWorkSubmitFile> wrapper = new QueryWrapper<>();
            wrapper.eq("work_id", sf.getId());
            List<BikeWorkSubmitFile> bikeWorkSubmitFiles = bikeWorkSubmitFileMapper.selectList(wrapper);
            //判断集合不为空
            if (bikeWorkSubmitFiles != null && !bikeWorkSubmitFiles.isEmpty()) {
                for (BikeWorkSubmitFile bfs : bikeWorkSubmitFiles) {
                    //判断连接不为空
                    if (StringUtils.isNotEmpty(bfs.getFileUrl())) {
                        if (sf.getFileUrl() != null) {
                            sf.setFileUrl(sf.getFileUrl() + "," + domain + bfs.getFileUrl());
                        } else {
                            sf.setFileUrl(domain + bfs.getFileUrl());
                        }
                    }
                }
            }
        }
        bikeWorkSweeps.sort(new Comparator<BikeWorkSweep>() {
            @Override
            public int compare(BikeWorkSweep o1, BikeWorkSweep o2) {
                return o2.getCreateTime().compareTo(o1.getCreateTime());
            }
        });
        return Result.OK(bikeWorkSweeps);
    }

    @Override
    public Result<?> pageListById(String id) {
        BikeWorkSweep bikeWorkSweep = bikeWorkSweepMapper.pageListById(id);
        if (bikeWorkSweep.getIsSolve().equals(BikeConstant.y_n_yes)||bikeWorkSweep.getActualTime()!=null){
            bikeWorkSweep.setIsFinish(BikeConstant.y_n_yes);
        }else {
            bikeWorkSweep.setIsFinish(BikeConstant.y_n_no);
        }
        QueryWrapper<BikeWorkSubmitFile> wrapper = new QueryWrapper<>();
        List<BikeWorkSubmitFile> bikeWorkSubmitFiles = bikeWorkSubmitFileMapper.selectList(wrapper);
        for (BikeWorkSubmitFile bfs : bikeWorkSubmitFiles){
            if (bikeWorkSweep.getId().equals(bfs.getWorkId())){
                if (bikeWorkSweep.getFileUrl()!=null){
                    bikeWorkSweep.setFileUrl(bikeWorkSweep.getFileUrl()+","+bfs.getFileUrl());
                }else {
                    bikeWorkSweep.setFileUrl(bfs.getFileUrl());
                }
            }
        }
        return Result.OK(bikeWorkSweep);
    }

    @Override
    public Result<?> deleteById(String id) {
        BikeWorkSweep bikeWorkSweep = bikeWorkSweepMapper.selectById(id);
        if (bikeWorkSweep.getIsDispatch().equals(BikeConstant.y_n_yes)){
            return Result.error("派单无法删除");
        }
        bikeWorkSweepMapper.deleteById(id);
        return Result.OK("删除成功");
    }

    @Override
    public ModelAndView exportXls(BikeWorkSweep bikeWorkSweep, HttpServletRequest request) {
        List<BikeWorkSweep> bikeWorkSweeps = bikeWorkSweepMapper.pageList(bikeWorkSweep);
        QueryWrapper<BikeWorkSubmitFile> wrapper = new QueryWrapper<>();
        List<BikeWorkSubmitFile> bikeWorkSubmitFiles = bikeWorkSubmitFileMapper.selectList(wrapper);
        for (BikeWorkSweep sf : bikeWorkSweeps){
            for (BikeWorkSubmitFile bfs : bikeWorkSubmitFiles){
                if (sf.getId().equals(bfs.getWorkId())){
                    if (sf.getFileUrl()!=null){
                        sf.setFileUrl(sf.getFileUrl()+","+bfs.getFileUrl());
                    }else {
                        sf.setFileUrl(bfs.getFileUrl());
                    }
                }
            }
        }
        bikeWorkSweeps.sort(new Comparator<BikeWorkSweep>() {
            @Override
            public int compare(BikeWorkSweep o1, BikeWorkSweep o2) {
                return o2.getCreateTime().compareTo(o1.getCreateTime());
            }
        });
        return ExcelUtils.exportXls(request,bikeWorkSweeps,BikeWorkSweep.class,"保洁工单");
    }

    @Override
    public String judgeDelet(String[] split) {
        List<String> strings = Arrays.asList(split);
        for (String tb : strings){
            BikeWorkSweep workSweep = bikeWorkSweepMapper.selectById(tb);
            if (workSweep!=null){
                String isDispatch = workSweep.getIsDispatch();
                if (isDispatch.equals(BikeConstant.y_n_yes)){
                    return BikeConstant.y_n_no;
                }
            }
        }
        return BikeConstant.y_n_yes;
    }

    @Override
    public String judgeDispatch(BikeWorkSweep bikeWorkSweep) {
        BikeWorkSweep workSweep = bikeWorkSweepMapper.selectById(bikeWorkSweep);
        if (workSweep.getIsDispatch().equals(BikeConstant.y_n_yes)){
            return BikeConstant.y_n_no;
        }else {
            return BikeConstant.y_n_yes;
        }
    }
}
